package com.tentime.saas.component.shiro;


import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

public class MultiTenantAuthenticationFilter extends FormAuthenticationFilter{
	
	public static final String SHIRO_FORM_TENANT_NAME_KEY = "tenant";
	
	@Override
	protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) {
		String username = getUsername(request);
        String password = getPassword(request);
        String tenant = obtainTenant(request);

        if (username == null) {
            username = "";
        }
        if (password == null) {
            password = "";
        }
        if (tenant == null) {
            tenant = "";
        }
        
        return new TenantAuthenticationToken(username, password, tenant);
    }
	
	private String obtainTenant(ServletRequest request) {
        return request.getParameter(SHIRO_FORM_TENANT_NAME_KEY);
    }
}
